package com.niiwoo.civet.trade.listener;

import com.alibaba.fastjson.JSON;
import com.niiwoo.civet.trade.constant.MqConstant;
import com.niiwoo.civet.trade.dto.common.RefundFreezeResultMsgDTO;
import com.niiwoo.civet.trade.service.local.newRefund.refundDeal.core.FreezeDealingService;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * 还款/垫付 冻结结果监听端
 * Created by wangjichao on 2018/4/25.
 */
@Slf4j
@Component
public class RefundFreezeResultListener {

    @Autowired
    private FreezeDealingService freezeDealingService;

    @RabbitListener(queues = {MqConstant.REFUND_FREEZE_RESULT_QUEUE})
    public void freezeResult(RefundFreezeResultMsgDTO resultMsgDTO, Message message, Channel channel) throws IOException {
        log.info("还款垫付冻结结果消息:{}", JSON.toJSONString(resultMsgDTO));
        try{
            freezeDealingService.freezeDeal(resultMsgDTO.getOrderId(), resultMsgDTO.getFreezeResult(),
                    resultMsgDTO.getFreezeRequestNo(), null, resultMsgDTO.getErrorCode(), resultMsgDTO.getErrorMessage());
        } catch (Exception e){
            log.error("还款垫付冻结结果消息处理失败:{}", JSON.toJSONString(resultMsgDTO), e);
        } finally {
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        }

    }
}
